﻿  //var imgpath = '../Common/Images/'; //이미지 경로
  var imgpath = 'http://localhost/Updms/Common/Images/'; //이미지 경로
  if (typeof(_isIE) == 'undefined') var _isIE=(navigator.userAgent.match(/Gecko/))?false:true;
  var msgBox = {
	msgobj: null,
	bglayer: null,
	msgtitle: null,
	msgdesc: null,
	msgicon: null,
	msgbtn: null,
	blnDrag: false,
	curmove: {'x': 0, 'y:': 0},
	returnValue: false,
	returnFunction: null,
	paramValue: null,
	keyCode: null,
	initDesign: function() {
		if (document.body) var b = document.body;
		else if (document.documentElement) var b = document.documentElement;
		if (b.nodeName != 'BODY') {
			for (var i=0;i<b.childNodes.length; i++) { if (b.childNodes[i].nodeName == 'BODY') { b = b.childNodes[i]; break; } }
		}
		if (b.nodeName != 'BODY') return false;
		var obj = document.createElement('DIV');
		var t = obj.cloneNode(false);
		var _t = obj.cloneNode(false);
		var _m = obj.cloneNode(false);
		var _b = obj.cloneNode(false);
		obj.id = 'msgBox';
		b.appendChild(obj);
		this.msgobj = obj;
		_t.className = 'msgBox_t';
		_m.className = 'msgBox_ml';
		_b.className = 'msgBox_b';
		obj.appendChild(_t);
		obj.appendChild(_m);
		obj.appendChild(_b);
		t.className = 'msgBoxtitle';
		
		t.onmousedown = function (e) { 
			msgBox.dragStart(e); 
		};
		this.msgtitle = t;
		_t.appendChild(t);
		var t = document.createElement('IMG');
		var img = t.cloneNode(false);
		this.msgicon = img;
		img.className = 'msgBoximg';
		img.style.display = 'none';
		t.className = 'msgBoxclose';
		t.src = imgpath+'close.gif';

		t.onclick = function () { 
			msgBox.setValue('close');
			msgBox.hide();
		};
		_t.appendChild(t);

		var t = _m.cloneNode(false);
		t.className = 'msgBox_mr';
		_m.appendChild(t);
		_m = t;
		var t = _m.cloneNode(false);
		t.className = 'msgBox_mm';
		_m.appendChild(t);
		_m = t;
		_m.appendChild(img);
		var t = this.msgtitle.cloneNode(false);
		t.className = 'msgBoxmsg';
		_m.appendChild(t);
		this.msgdesc = t;
		var t = this.msgtitle.cloneNode(false);
		t.className = 'msgBoxbtn';
		_m.appendChild(t);
		this.msgbtn = t;
		delete t,obj,img,_t,_m,_b;
		return true;
	},
	dragStart: function (event) {
		msgBox.blnDrag = true;
		if (_isIE) {
			var e = window.event;
			window.document.attachEvent('onmousemove', msgBox.draging);
			window.document.attachEvent('onselectstart', msgBox.cancelEvent);
			window.document.attachEvent('onmouseup', msgBox.dragEnd);
		}
		else {
			var e = event;
			window.document.addEventListener('mousemove', msgBox.draging, false);
			window.document.addEventListener('mouseup', msgBox.dragEnd, false);
		}
		//alert(e.keyCode);
		msgBox.curmove.x = parseInt(e.clientX);
		msgBox.curmove.y = parseInt(e.clientY);
	},
	dragEnd: function (e) {
		if (_isIE) window.document.detachEvent('onmousemup', msgBox.dragEnd);
		else window.document.removeEventListener('mouseup', msgBox.dragEnd, false);
		msgBox.blnDrag = false;
	},
	draging: function (e) {
		if (msgBox.blnDrag == true) {
			if (parseInt(e.clientX - msgBox.curmove.x) > 5 || parseInt(e.clientX - msgBox.curmove.x) < -5) {
				msgBox.msgobj.style.left = parseInt(e.clientX) - parseInt(msgBox.msgobj.offsetWidth) / 3 + document.documentElement.scrollLeft + 'px';
				msgBox.curmove.x = e.clientX;
			}
			if (parseInt(e.clientY - msgBox.curmove.y) > 5 || parseInt(e.clientY - msgBox.curmove.y) < -5) {
				msgBox.msgobj.style.top = parseInt(e.clientY) - 15 + document.documentElement.scrollTop + 'px';
				msgBox.curmove.y = e.clientY;
			}
		}
	},
	cancelEvent: function (e) {
		return false;
	},
	resizecheck: function () {
		msgBox.bglayer.style.height = (document.body.offsetHeight > document.documentElement.offsetHeight ? document.body.offsetHeight : document.documentElement.offsetHeight) + 'px';
		msgBox.bglayer.style.width = (document.body.offsetWidth > document.documentElement.offsetWidth ? document.body.offsetWidth : document.documentElement.offsetWidth) + 'px';
	},
	bgshow: function (val) {
		if (this.bglayer == null) {
			var obj = document.createElement('DIV');
			obj.className = 'msgBoxbg';
			try {obj.style.opacity = 0.8;} catch(e) {}
			try {obj.style.MozOpacity = 0.8;} catch(e) {}
			try {obj.style.filter = 'alpha(opacity=80)';} catch(e) {}	//배경투명도
			try {obj.style.KhtmlOpacity = 0.8;} catch(e) {}
			document.body.appendChild(obj);
			this.bglayer = obj;
		}
		if (val == 1) {
			var _height = this.trueBody().scrollHeight;
			var _width = this.trueBody().scrollWidth;
			_height = _height > this.trueBody().clientHeight ? _height : this.trueBody().clientHeight;
			_width = _width > this.trueBody().clientWidth ? _width : this.trueBody().clientWidth;
			this.bglayer.style.height = _height + 'px';
			this.bglayer.style.width = _width + 'px';
			this.bglayer.style.display = 'block';
		} 
		else {
			this.bglayer.style.display = 'none';
		}
	},	
	init: function (getTitle, getMsg, getBtn, getIcon, returnObj, param, keyCode) {
		if (this.msgobj == null) var result = this.initDesign();
		if (result == false) { return false; }
		if (!getTitle) getTitle = 'MessageBox Title';
		if (!getMsg) getMsg = 'MessageBox Description';
		if (!getBtn) getBtn = 'OK';
		if (!getIcon) getIcon = 1;	//미지정시 기본 아이콘
		this.msgobj.style.left = '40%';
		this.msgobj.style.top = '40%';
		this.msgtitle.innerHTML = getTitle;
		this.msgdesc.innerHTML = getMsg.replace(/\n/g,'<br />');
		this.returnFunction=null;
		if (returnObj != null) this.returnFunction = returnObj;
		this.paramValue=null;	//함수호출시 파라미터도 같이 넘기도록
		if (param != null) this.paramValue = param;
		this.keyCode=null;	//넘어온 keycode를 저장
		if (keyCode != null) this.keyCode = keyCode;

		//위치 top
		if (document.documentElement.scrollTop != 0) {
			this.msgobj.style.top = document.documentElement.scrollTop + (document.documentElement.offsetHeight/3) + 'px';
		} else if (document.body.scrollTop != 0) { 
			this.msgobj.style.top = document.body.scrollTop + (document.body.offsetHeight/4) + 'px';
		}

		//위치 left
		if (document.documentElement.scrollLeft != 0) {
			this.msgobj.style.left = document.documentElement.scrollLeft + (document.documentElement.offsetWidth/3) + 'px';
		} else if (document.body.scrollLeft != 0) {
			this.msgobj.style.left = document.body.scrollLeft + (document.body.offsetWidth/4) + 'px';
		}

		//40%로는 메시지창이 가운데 안오는 경우
		var _height = this.trueBody().clientHeight;
		if (_height < 800) {
			this.msgobj.style.top = _height/2-60 + 'px';
		}
		var _width = this.trueBody().clientWidth;
		if (_height < 800) {
			this.msgobj.style.left = _width/2-145 + 'px';
		}

		if (getIcon != 0 && getIcon < 6) {
			if (getIcon == 1) this.msgicon.src = imgpath+'msg_info.png';
			else if (getIcon == 2) this.msgicon.src = imgpath + 'msg_question.png';
			else if (getIcon == 3) this.msgicon.src = imgpath + 'msg_warning.png';
			else if (getIcon == 4) this.msgicon.src = imgpath + 'msg_error.png';
			else if (getIcon == 5) this.msgicon.src = imgpath + 'msg_exit.png';
			this.msgicon.style.display = 'block';
		}
		var btnstr = '';
		var btnarr = getBtn.split(';')[0].split('|');
		var cnt = btnarr.length;
		for (var i=0;i<cnt;i++) { 
			btnstr += '<input id="msgBoxBtn" type="button" class="msgBoxbtnin" onclick="msgBox.setValue(this.value);msgBox.hideInit();" value="' + btnarr[i] + '">';
		}
		this.msgbtn.innerHTML = btnstr;
		return true;
	},
	show: function (title, msg, returnObj, btn, iconType) {
		var result = this.init(title, msg, btn, iconType, returnObj, null, null);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
			if (btn != undefined && btn.split(';')[1] != undefined) this.setFocus(btn.split(';')[1]);
			else this.setFocus();
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
    //정보창 보여주고 함수호출
	info: function (msg, returnObj) {
		var result = this.init(getMsg(MSG_STR_005), msg, "OK", 1, returnObj, null, null);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
    //정보창 보여주고 함수호출시 파라미터도 넘김
	info: function (msg, returnObj, param) {
	    var result = this.init(getMsg(MSG_STR_005), msg, "OK", 1, returnObj, param, null);
	    if (result == true) {
	        this.bgshow(1);
	        if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
	        else window.addEventListener('resize', msgBox.resizecheck, false);
	        this.msgobj.style.display = 'block';
	    }
	    else { window.alert(msg); }
	    this.setFocus("msgBoxBtn");
	},
	//경고창
	alert: function (msg) {
		var result = this.init(getMsg(MSG_STR_007), msg, null, 3, null, null, null);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
	//경고창 보여주고 함수호출
	alert: function (msg, returnObj) {
		var result = this.init(getMsg(MSG_STR_007), msg, null, 3, returnObj, null, null);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
	//경고창 보여주고 함수호출시 파라미터도 넘김
	alert: function (msg, returnObj, param) {
		var result = this.init(getMsg(MSG_STR_007), msg, null, 3, returnObj, param, null);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
	//경고창 보여주고 함수호출시 파라미터도 넘기고 엔터값 처리
	alert: function (msg, returnObj, param, keyCode) {
		var result = this.init(getMsg(MSG_STR_007), msg, null, 3, returnObj, param, keyCode);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
	//확인창 - returnObj에서 getValue()로 '예' 또는 'Yes' 또는 'Y'로 판단
	confirm: function (msg, returnObj) {
		var result = this.init(getMsg(MSG_STR_006), msg, getMsg(MSG_STR_010), 2, returnObj, null, null);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
	//확인창 - returnObj에서 getValue()로 '예' 또는 'Yes' 판단, 파라미터도 넘김
	confirm: function (msg, returnObj, param) {
		var result = this.init(getMsg(MSG_STR_006), msg, getMsg(MSG_STR_010), 2, returnObj, param, null);
		if (result == true) {
		this.bgshow(1);
		if (_isIE) window.attachEvent('onresize', msgBox.resizecheck);
		else window.addEventListener('resize', msgBox.resizecheck, false);
		this.msgobj.style.display = 'block';
		}
		else { window.alert(msg); }
		this.setFocus("msgBoxBtn");
	},
	//숨기기전에 엔터값 확인
	hideInit: function() {
		if(this.keyCode != 13){
			this.hide();
		}
		this.keyCode = null;
	},
	hide: function() {
		this.bgshow(0);
		this.msgobj.style.display = 'none';
		if (_isIE) {
			window.document.detachEvent('onmousemove', msgBox.draging);
			window.document.detachEvent('onselectstart', msgBox.cancelEvent);
			window.document.detachEvent('onmouseup', msgBox.dragEnd);
			window.detachEvent('onresize', msgBox.resizecheck);
		}
		else {
			window.document.removeEventListener('mousemove', msgBox.draging, false);
			window.document.removeEventListener('mouseup', msgBox.dragEnd, false);
			window.removeEventListener('resize', msgBox.resizecheck, false);
		}

		if (this.returnFunction != null) this.returnFunction(this.paramValue);
	},	
	setValue: function(val) {
		if (typeof(val) == 'string') this.returnValue = val;
		else this.returnValue = val;
	},
	getValue: function() {
		return this.returnValue;
	},
	setFocus: function(val) {
		if (!val) {
			if (this.msgbtn.hasChildNodes() == false) return false;
			if (this.msgbtn.firstChild.nodeName == 'INPUT') this.msgbtn.firstChild.focus();
			else this.msgbtn.firstChild.nextSibling.focus();
			return true;
		}
		else {
			if (this.msgbtn.hasChildNodes() == false) return false;
			for (var i=0;i<this.msgbtn.childNodes.length;i++) { if (this.msgbtn.childNodes[i].nodeName == 'INPUT' && this.msgbtn.childNodes[i].value == val) { this.msgbtn.childNodes[i].focus(); return true; } }
			if (this.msgbtn.firstChild.nodeName == 'INPUT') this.msgbtn.firstChild.focus();
			else this.msgbtn.firstChild.nextSibling.focus();
		}
	},
	trueBody: function () {
		return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement: document.body;
	}
  };